import { createStore } from "vuex";

export interface IModal {
  isShowModal: boolean;
  title: string;
  cancelBtnText?: string;
  confirmBtnText?: string;
  success?: (e: any) => void;
  cancel?: (e: any) => void;
}
interface IModalPayload extends IModal {
  cancelBtnText: string;
  confirmBtnText: string;
}

export default createStore({
  state: {
    isShowLoading: false,   // 请求loading状态
    isShowDialogue: false,  // 是否显示dialogue
    dialogueMsg: "",        // dialogue内容
    modalParams: {          // 弹窗
      isShowModal: false,
      title: "",
      cancelBtnText: "",
      confirmBtnText: "",
    },
    cancel: (e: any): void => { },
    success: (e: any): void => { },
    currentDataId: 0, // 非口语综合 data_id
    courseId: 0, // 课程course_id
  },
  mutations: {
    setLoading(state, payload: boolean) {
      state.isShowLoading = payload;
    },
    setDialogueMsg(state, payload: string) {
      state.dialogueMsg = payload;
    },
    setDialogueIsShow(state, payload: boolean) {
      state.isShowDialogue = payload;
    },
    setModalIsShow(state, payload: IModalPayload) {
      state.modalParams = payload;
    },
    setCurrentDataId(state, payload: number) {
      state.currentDataId = payload;
    },
    setCourseId(state, payload: number) {
      state.courseId = payload;
    },
  },
  actions: {
    setLoadingAction(context, payload) {
      context.commit("setLoading", payload);
    },
    setDialogueMsg(context, payload) {
      context.commit("setDialogueMsg", payload);
    },
    setDialogueIsShow(context, payload) {
      context.commit("setDialogueIsShow", payload);
    },
    setModalIsShow(context, payload) {
      context.commit("setModalIsShow", payload);
      context.state.cancel = payload.cancel;
      context.state.success = payload.success;
    },
    toggleSuccess(context, payload) {
      context.state.success(payload);
    },
    toggleCancel(context, payload) {
      context.state.cancel(payload);
    },
    setCurrentDataId(context, payload) {
      context.commit("setCurrentDataId", payload);
    },
    setCourseId(context, payload) {
      context.commit("setCourseId", payload);
    },
  }
})